package com.lijiajia.cloud.resource.sys.config.service;

import com.lijiajia.cloud.resource.sys.service.numberid.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * numberid 服务配置
 *
 * @author lijiajia
 * @since 2019-05-24
 */
@Configuration
public class NumberIdConfiguration {
    private final RedisNumberIdService redisNumberIdService;
    private final MemoryNumberIdService memoryNumberIdService;
    private final DateNumberIdService dateNumberIdService;

    public NumberIdConfiguration(RedisNumberIdService redisNumberIdService,
                                 MemoryNumberIdService memoryNumberIdService,
                                 DateNumberIdService dateNumberIdService) {
        this.redisNumberIdService = redisNumberIdService;
        this.memoryNumberIdService = memoryNumberIdService;
        this.dateNumberIdService = dateNumberIdService;
    }

    /**
     * 容错生成
     * 1. redis 方式生成 id
     * 2. date时间戳方式生成 id
     *
     * @return 数字id服务
     */
    @Bean
    public DelegatingNumberIdService delegatingNumberIdService() {
        List<NumberIdService> services = new ArrayList<>();
        services.add(redisNumberIdService);
        // services.add(memoryNumberIdService);
        services.add(dateNumberIdService);
        return new DelegatingNumberIdService(services);
    }
}
